﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using QuanLyDiem.models;
using QuanLyDiem.Controllers;
namespace QuanLyDiem.DanhMuc
{
    public partial class CTDaoTao : DevExpress.XtraEditors.XtraForm
    {
        public CTDaoTao()
        {
            InitializeComponent();
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, false, false, false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            C_Khoa.LoadDataSource(lueKhoa, db);
            C_Khoa.LoadDataSource(rlueID_Khoa, db);
            C_Mon.LoadDataSource(lueMon, db);
            C_Mon.LoadDataSource(rlueMon, db);
            C_CTDaoTao.LoadBindingSource(chuongTrinhDaoTaoBindingSource);
            gridView1.BestFitAllColumns();


        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var daotao = gridView1.GetFocusedRow() as ChuongTrinhDaoTao;
            if (daotao == null) return;
            lueKhoa.EditValue = daotao.ID_khoa;
            lueMon.EditValue = daotao.ID_mon;
            txtkyhoc.Text = daotao.Ky_hoc.ToString();


        }
       
        private void btnthem_Click(object sender, EventArgs e)
        {
           
            btnsua.Enabled = btnxoa.Enabled = false;
            txtkyhoc.Text = "";
            lueKhoa.ItemIndex = 0;
            lueMon.ItemIndex = 0;
            txtkyhoc.Focus();
            
        }

        private void btnthuchien_Click(object sender, EventArgs e)
        {
            try
            {
                var daotao = new ChuongTrinhDaoTao()
                {
                    Ky_hoc = int.Parse(txtkyhoc.Text),
                    ID_khoa = lueKhoa.EditValue == null ? 0 : (int)lueKhoa.EditValue,
                    ID_mon = lueMon.EditValue == null ? 0 : (int)lueMon.EditValue
                };
                if (!C_CTDaoTao.Insert(daotao)) throw new Exception();
                C_CTDaoTao.LoadBindingSource(chuongTrinhDaoTaoBindingSource);
                gridView1.BestFitAllColumns();
                btnsua.Enabled = btnxoa.Enabled = true;

            }
            catch (Exception)
            {

                MessageBox.Show("Lỗi");
            }

            
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                var daotao = gridView1.GetFocusedRow() as ChuongTrinhDaoTao;
                daotao.Ky_hoc = int.Parse(txtkyhoc.Text);
                daotao.ID_khoa = lueKhoa.EditValue == null ? 0 : (int)lueKhoa.EditValue;
                daotao.ID_mon = lueMon.EditValue == null ? 0 : (int)lueMon.EditValue;
                if (!C_CTDaoTao.Update(daotao)) throw new Exception();
                C_CTDaoTao.LoadBindingSource(chuongTrinhDaoTaoBindingSource);
                gridView1.BestFitAllColumns();
                
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }

        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            try
            {
                var daotao = gridView1.GetFocusedRow() as ChuongTrinhDaoTao;
                if (!C_CTDaoTao.Delete(daotao)) throw new Exception();
                C_CTDaoTao.LoadBindingSource(chuongTrinhDaoTaoBindingSource);
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}